<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header('新增数据库配置')" />
	<th:block th:include="include :: select2-css" />
	<th:block th:include="include :: bootstrap-select-css" />
</head>
<body class="white-bg">
	<div class="wrapper wrapper-content animated fadeInRight ibox-content">
		<form class="form-horizontal m" id="form-post-add">
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">connectName：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="connectName" id="connectName" required>
				</div>
			</div>

			<div class="form-group">
				<label class="col-sm-3 control-label is-required">type：</label>
				<div class="col-sm-8">
					<select class="form-control"  name="type" id="type"  required>
						<option value="">--请选择数据库类型--</option>
						<option th:each="typeList:${dbtypes}" th:value="${typeList}" th:text="${typeList}"></option>
					</select>
				</div>
			</div>

			<div class="form-group">
				<label class="col-sm-3 control-label is-required">url：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="url" id="url" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">userName：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="userName" id="userName" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">pwd：</label>
				<div class="col-sm-8">
					<input class="form-control" type="password" name="pwd" id="pwd" required>
				</div>
			</div>
		</form>
	</div>
	<th:block th:include="include :: footer" />
	<script type="text/javascript">
		var prefix = ctx + "system/dbconfig";

		$("#form-post-add").validate({
			onkeyup: false,
			rules:{
				connectName:{
					remote: {
		                url: ctx + "system/dbconfig/checkConnectNameUnique",
		                type: "post",
		                dataType: "json",
		                data: {
		                	"connectName" : function() {
		                        return $.common.trim($("#connectName").val());
		                    }
		                },
		                dataFilter: function(data, type) {
		                	return $.validate.unique(data);
		                }
		            }
				},
			},
			messages: {
		        "connectName": {
		            remote: "connectName已经存在"
		        }
		    },
		    focusCleanup: true
		});

		function submitHandler() {
	        if ($.validate.form()) {
				testConnection(prefix + "/testConnection", $('#form-post-add').serialize());
	        }
	    }

		function testConnection(url, data, callback) {
			var config = {
				url: url,
				type: "post",
				dataType: "json",
				data: data,
				beforeSend: function () {
					$.modal.loading("正在处理中，请稍候...");
				},
				success: function(result) {
					if (typeof callback == "function") {
						callback(result);
					}
					if (result.code == web_status.SUCCESS) {
						//$.modal.alertSuccess(result.msg)
						$.operate.save(prefix + "/add", $('#form-post-add').serialize());
					} else if (result.code == web_status.WARNING) {
						$.modal.alertWarning(result.msg)
					} else {
						$.modal.alertError(result.msg);
					}
					$.modal.closeLoading();
				}
			};
			$.ajax(config)
		}

	</script>
</body>
</html>
